//----------------------------------------------------------------------------
// View 'Apotheek selecteren'
//----------------------------------------------------------------------------
var selectedApotheek = '';

var viewApotheek = Titanium.UI.createView({
   backgroundColor : 'transparent',
   width : '100%',
   height : '100%'
});

//----- header
if (isIpad()) {
   var viewApotheekPickerHeader = Titanium.UI.createView({
      top : '110',
      backgroundColor : donkergroen,
      width : '400',
      height : '35',
      borderRadius : '2',
      borderColor : donkergroen
   });
}
else if (isIphone()) {
   var viewApotheekPickerHeader = Titanium.UI.createView({
      top : '10',
      backgroundColor : donkergroen,
      width : '95%',
      height : '35',
      borderRadius : '2',
      borderColor : donkergroen
   });
}
viewApotheekPickerHeader.add(Titanium.UI.createLabel({
   textAlign : Ti.UI.TEXTALIGNMENTLEFT,
   font : {
      fontSize : 12,
      fontWeight : 'bold',
      fontFamily : customFont
   },
   color : wit,
   top : '5',
   left : '5',
   right : '5',
   text : T_HHRECEPT_DLG_PHARMACYPICKER_TITLE
}));

var btnPickApotheek = Titanium.UI.createButton({
   top : '1',
   right : '1',
   width : '100',
   height : '25',
   font : {
      fontSize : 14,
      fontWeight : 'bold',
      fontFamily : customFont
   },
   color : wit,
   title : T_HHRECEPT_DLG_PHARMACYPICKER_SELECT,
   backgroundColor : donkergroen,
   backgroundImage : 'images/backgrounds/bg-donkergroen.png',
   borderColor : donkergroen
});
btnPickApotheek.addEventListener('click', function(e) {
   selectedApotheek = pickerApotheek.getSelectedRow(0).title;
   winHHRecept.remove(viewApotheek);
   tfApotheek.value = selectedApotheek;
});

viewApotheekPickerHeader.add(btnPickApotheek);

//----- Content
if (isIphone()) {
   var viewApotheekPickerContent = Titanium.UI.createView({
      backgroundColor : wit,
      top : '35',
      width : '95%',
      height : '215',
      borderRadius : '2',
      borderColor : donkergroen
   });
}
else if (isIpad()) {
   var viewApotheekPickerContent = Titanium.UI.createView({
      backgroundColor : wit,
      top : '135',
      width : '400',
      height : '215',
      borderRadius : '2',
      borderColor : donkergroen
   });
}
var pickerApotheek = Titanium.UI.createPicker({
   top : '0',
   width : '100%',
   useSpinner : true,
   selectionIndicator: true
});

pickerApotheek.add(getApotheken());

viewApotheekPickerContent.add(pickerApotheek);
viewApotheek.add(viewApotheekPickerHeader, viewApotheekPickerContent);


//----- Functions
function getApotheken() {
   log("Lijst apotheken ophalen uit database");
   apothekendb = Ti.Database.open('apotheken');
   
   var collection = [];
   var row = 0;
   var apothekenRS = apothekendb.execute('SELECT name FROM apotheken');
   
   while (apothekenRS.isValidRow()) {
      apotheekName = apothekenRS.fieldByName('name');
      collection[row] = Ti.UI.createPickerRow({
         title: apothekenRS.fieldByName('name')
      });
      row++;
      apothekenRS.next();
   }
   apothekenRS.close();
   apothekendb.close();
   
   return collection;
}
